package com.heima.controller;

import com.heima.common.Result;
import com.heima.oss.AliyunOSSOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@Api("上传接口")
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

//    @PostMapping(value = "/api/vm-service/sku/fileUpload", consumes = "multipart/form-data")
//    @ApiOperation("上传文件")
//    public String upload(@RequestParam(value = "fileName", required = false) MultipartFile fileName) {
//        log.info("文件上传：{}", fileName);
//
//        // 检查文件是否为空
//        if (fileName == null || fileName.isEmpty()) {
//            throw  new RuntimeException("文件不能为空");
//
//        }
//
//        try {
//            //原始文件名
//            String originalFilename = fileName.getOriginalFilename();
//            //截取原始文件名的后缀
//            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//            //构造新文件名称
//            String objectName = UUID.randomUUID().toString() + extension;
//
//            //文件的请求路径
//            String filePath = aliyunOSSOperator.upload(fileName.getBytes(), objectName);
//            return filePath;
//        } catch (Exception e) {
//            log.error("文件上传失败：{}", e.getMessage());
//            throw new RuntimeException("文件上传失败");
//
//        }
//    }

    @PostMapping(value = "/api/vm-service/sku/fileUpload", consumes = "multipart/form-data")
    @ApiOperation("上传文件")
    public String upload(@RequestParam("fileName") MultipartFile fileName) { // required=false 可以去掉，让 Spring 处理
        log.info("文件上传：{}", fileName.getOriginalFilename());

        if (fileName.isEmpty()) {
           throw new  RuntimeException("文件不能为空");
        }

        try {
            String originalFilename = fileName.getOriginalFilename();
            // 改进后缀提取
            String extension = "";
            int dotIndex = originalFilename.lastIndexOf('.');
            if (dotIndex >= 0) {
                extension = originalFilename.substring(dotIndex); // 包含 .
            } else {
                // 可以根据 contentType 推断，或使用默认后缀
                log.warn("文件没有后缀名: {}", originalFilename);
            }
            String objectName = UUID.randomUUID().toString() + extension;

            String filePath = aliyunOSSOperator.upload(fileName.getBytes(), objectName);
            if (filePath == null || filePath.trim().isEmpty()) {
               throw new RuntimeException("文件上传失败");
            }
            return filePath;
        } catch (IOException e) {
            log.error("文件读取失败", e);
            return "文件上传失败";
        } catch (Exception e) {
            log.error("文件上传失败", e);
            throw new RuntimeException("文件上传失败");
        }
    }





}
